/*****************************************************************************/
/**
*
* @file uinterrupt.c
*
* General functions about scugic, exceptions and interrupts.
*
* Ver   Who    Date   	Changes
* ----- ---- ---------- -------------------------------------------------------
* 0.01  abu  01/28/2019 File created.
* 0.10	abu  07/10/2019	Completed first version(costs about half a year, lose face T_T!).
* </pre>
*
*------------------------------------------------------------------------------
* Author: abu	E-mail:	abu_liu@aliyun.com
*				Blog:	https://blog.csdn.net/ClamerKorallen
*				Gitee:	https://gitee.com/abu_liu
*------------------------------------------------------------------------------
*
******************************************************************************/


/***************************** Include Files *********************************/
#include "uinterrupt.h"

/************************** Constant Definitions *****************************/

/**************************** Type Definitions *******************************/

/***************** Macros (Inline Functions) Definitions *********************/

/************************** Variable Definitions *****************************/

/************************** Function Prototypes ******************************/

/*****************************************************************************/
/**
*
* Initialize scugic.
*
* @param	InstancePtr: pointer to XScuGic device
*
* @return	None.
*
* @note		1. initialize scugic related variables and pointers.
			2. connect shared peripheral interrupt handlers to XScuGic_InterruptHandler.
			3. register XScuGic_InterruptHandler to exception vector IRQInterrupt.
*
******************************************************************************/

void uScuGicInit(void)
{
	int status;
	XScuGic *InstancePtr = &uScuGic;

	Xil_ExceptionInit();

	InstancePtr->Config = XScuGic_LookupConfig(SCUGIC_DEVID);
	if (NULL == InstancePtr->Config) {
		DEBUG("\n\rGIC initialization failed\n\r");
		DEBUG_POS();
	}

	status = XScuGic_CfgInitialize(InstancePtr, InstancePtr->Config, InstancePtr->Config->CpuBaseAddress);	
	if (status != XST_SUCCESS) {
		DEBUG("\n\rGIC initialization failed\n\r");
		DEBUG_POS();
	}

	//register scugic interrupt handler to exception vector IRQ
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, InstancePtr);

	//enable exception
	Xil_ExceptionEnable();
}
